﻿using SuperSocket.Facility.Protocol;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SuperSocketServer.SSBase
{
    /// <summary>
    /// 自定义包筛选协议
    /// </summary>
    public class MyReceiveFilter : FixedHeaderReceiveFilter<MyRequestInfo>
    {
        /// +-------+---+-------------------------------+
        /// |request| l |                               |
        /// | name  | e |    request body               |
        /// |  (2)  | n |                               |
        /// |       |(2)|                               |
        /// +-------+---+-------------------------------+
        /// <summary>
        /// 定义头部有四个字节
        /// </summary>
        public MyReceiveFilter():base(4)
        {

        }
        //返回 body的长度
        protected override int GetBodyLengthFromHeader(byte[] header, int offset, int length)
        {
            return header[offset + 2] * 256 + header[offset + 3];
        }

        //返回requestInfo对象
        protected override MyRequestInfo ResolveRequestInfo(ArraySegment<byte> header, byte[] bodyBuffer, int offset, int length)
        {
            var body = bodyBuffer.Skip(offset).Take(length).ToArray();
            return new MyRequestInfo(header.Array, body);
        }

    }
}
